package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.userMapper;
import com.baizhi.entity.user;
import com.baizhi.entity.userExample;
import com.baizhi.service.UserService;
import com.baizhi.util.ALiYunUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class UserServiceImpl implements UserService {


    @Resource
    userMapper userMapper;
    @Resource
    HttpServletRequest request;
    @AddCache
    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();
        //设置当前页
        map.put("page",page);
        //设置条件查询对象
        userExample example = new userExample();
        //查询总条数
        int count = userMapper.selectCountByExample(example);
        //设置总条数
        map.put("total",count);
        //创建分页对象   起始条数  数据数
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        //分页查询数据
        List<user> users = userMapper.selectByExampleAndRowBounds(example, rowBounds);
        //设置数据
        map.put("rows",users);
        return map;

    }
    @DelCache
    @AddLog(value = "删除用户")
    @Override
    public HashMap<String, Object> delete(user user) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            com.baizhi.entity.user users = userMapper.selectOne(user);
            String headImg = users.getHeadImg();
            String replace = headImg.replace("https://yingx-2013.oss-cn-beijing.aliyuncs.com/","");
            ALiYunUtil.deleteBucket("yingx-2013",replace);
            userMapper.deleteByPrimaryKey(user);
            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }

        return map;
    }
    @DelCache
    @AddLog(value = "修改用户")
    @Override
    public HashMap<String, Object> update(user user) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            userMapper.updateByPrimaryKeySelective(user);
            map.put("message","修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败！");
        }
        return map;
    }
    @DelCache
    @AddLog(value = "添加用户")
    @Override
    public HashMap<String, Object> add(user user) {
        HashMap<String, Object> map = new HashMap<>();
        user.setId(UUID.randomUUID().toString());
        try {
            userMapper.insertSelective(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    @Override
    public String uploadHeadImg(MultipartFile headImg) {
        //获取文件名
        String filename = headImg.getOriginalFilename();
        //修改名字  拼时间戳
        String newName =new Date().getTime()+"-"+filename;
        //获取绝对路径
        String realPath = request.getSession().getServletContext().getRealPath("/upload/headImg");
        File file = new File(realPath);
        //文件夹不存在
        if(!file.exists()){
            //创建文件夹
            file.mkdirs();
        }
        String message=null;
        //文件上传
        try {
            headImg.transferTo(new File(newName,realPath));
            message=newName;
        } catch (IOException e) {
            e.printStackTrace();
            message=newName;
        }
        return message;
    }

    @Override
    public String uploadHeadImgALiY(MultipartFile headImg) {
        //获取文件名
        String filename = headImg.getOriginalFilename();
        //修改名字  拼时间戳
        String newName =new Date().getTime()+"-"+filename;
        //存储空间名
        String bucketName = "yingx-2013";
        //配置文件名
        String fileName = "headImg/"+newName;

        String netpath = "https://yingx-2013.oss-cn-beijing.aliyuncs.com/"+fileName;

        String message=null;
        //文件上传
        try {
            ALiYunUtil.uploadFileBytes(bucketName,fileName,headImg);
            message=netpath;
        } catch (Exception e) {
            e.printStackTrace();
            message=netpath;
        }
        return message;
    }

    @Override
    public user queryById(String id) {

        return userMapper.selectByPrimaryKey(id);
    }
}
